/**外包企业门户网站,生成时间:2017-04-19 09:55:35**/
package com.xbdun.dao.bmp.mapper;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;

import com.xbdun.dao.bmp.dto.DepartNodeDto;
import com.xbdun.dao.bmp.entity.XbdunDepartDo;
import com.xbdun.dao.bmp.provider.DepartSqlProvider;


/**
 * 部门
 * @author zhanggt
 * @2017-04-19 09:55:35
 */
@Mapper
public interface DepartMapper{

	/**
	 * 根据主键获取对象
	 * @return
	 */
	@Select("SELECT * FROM xbdun_depart where code=#{code} AND orgCode=${orgCode}")
	public XbdunDepartDo getXbdunDepartByCode(@Param("code")long code,@Param("orgCode")long orgCode);
	
	/**
	 * 新增
	 * @param XbdunDepartDo 部门
	 */
	@InsertProvider(type=DepartSqlProvider.class,method="addXbdunDepartSql")
	public Long addXbdunDepart(@Param("a")XbdunDepartDo xbdunDepartDo);
	@Update("UPDATE xbdun_depart SET sort=#{code} WHERE id=#{id}")
	public int updateDepartSort(@Param("id")long id,@Param("code")long code);
	@Select("SELECT RPAD(code,18,'0') FROM xbdun_depart WHERE id=#{id}")
	public long selectToCode(@Param("id")long id);
	/**
	 * 删除数据根据主键
	 * @param id
	 */
	@Delete("DELETE FROM xbdun_depart WHERE code = ${code} AND orgCode = ${orgCode} AND version=#{version}")
	public int delDepartByCode(@Param("code")long code,@Param("orgCode")long orgCode,@Param("version")Timestamp version);
	
	/**
	 * 判断是否存在,不为null则是存在
	 * @param param
	 * @return Long
	 */
	@SelectProvider(type=DepartSqlProvider.class,method="existSql")
	public Long existXbdunDepart(@Param("param")Map<String,Object> param);
	/**
	 * 动态更新参数,参数中Map可以必须跟Key为Where的更新条件<br/>
	 * <pre>
	 * 	例如：
	 *  Map<String,Object> setting = new HashMap<String,Object>();
	 *  setting.put("email", "360730440@qq.com");
	 *
	 *  Map<String,Object> where = new HashMap<String,Object>();
	 *  where.put("userId", "1222");
	 * </pre>
	 * Map的key值就是数据库字段<br/>
	 * Map的value就是值
	 */
	@UpdateProvider(type=DepartSqlProvider.class,method="updateDynXbdunDepartSql")
	public int updateDynXbdunDepart(@Param("param")Map<String,Object> param,@Param("_where")Map<String,Object> where);
	
	//------------------------//start分页查询--------------------------------------
	/**
	 * 分页查询 
	 * <pre>
	 * 	例如：
	 *  Map<String,Object> where = new HashMap<String,Object>();
	 *  param.put("orderby", "param.name,param age desc");
	 * </pre>
	 * @param _where where条件，现在支持And条件类型
	 * @param orderBy 排序条件
	 * @param pageNum 当前索引页
	 * @param numPerPage 每页显示条数
	 * @return
	 */
	@SelectProvider(type=DepartSqlProvider.class,method="queryPagerSql")
	public List<XbdunDepartDo> queryPager(@Param("_where")Map<String,Object> where,@Param("_orderby")String orderBy,@Param("pageNum")int pageNum,@Param("numPerPage")int numPerPage);
	
	/**
	 * 分页查询返回总数量
	 * <pre>
	 * 	例如：
	 *  Map<String,Object> where = new HashMap<String,Object>();
	 *  param.put("orderby", "param.name,param age desc");
	 * </pre>
	 * @param where 参数为where约束参数
	 * @return
	 */
	@SelectProvider(type=DepartSqlProvider.class,method="queryPagerCountSql")
	public Long queryPagerCount(@Param("_where")Map<String,Object> where);
	//-------------------------//end--------------------------------------------

	/**
	* @Title: queryTreeDepart
	* @Description: 获取部门树状结构
	* @param orgCode			当前机构
	* @param xbdOrgCode			平台机构
	* @return List<DepartNodeDto>    返回类型
	*/
	@Select("SELECT code id, LEFT(code,LENGTH(code)-6) pId,departName name,remark title FROM xbdun_depart WHERE orgCode in (${orgCode},${xbdOrgCode}) ORDER BY sort ASC")
	public List<DepartNodeDto> queryTreeDepart(@Param("orgCode")long orgCode,@Param("xbdOrgCode")long xbdOrgCode);
}